package cn.jasonone.views;

import cn.jasonone.model.Student;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractUrlBasedView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Map;

/**
 * Excel视图处理器
 * @author xujie
 * @version 1.0.0
 */
public class ExcelView extends AbstractUrlBasedView {
    private final String absolutePath;

    public ExcelView(String absolutePath) {
        this.absolutePath = absolutePath;
    }

    @Override
    protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode("学生信息表.xlsx","UTF-8"));
        EasyExcel.write(response.getOutputStream())
                .withTemplate(absolutePath)
                .sheet("sheet1")
                .doFill(model.get("students"));
    }
}
